package game.gfx.cellmap;

import game.gfx.AbstractImageGeneratingGame;
import game.util.Logger;

import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

public abstract class AbstractCellMapGame<TYPE extends AbstractCellMap<?>> extends AbstractImageGeneratingGame
{
    private TYPE  map;
    private Point mousePoint = null;
    
    protected enum MouseEventType
    {
        MOVED, DRAGGED
    }
    
    protected AbstractCellMapGame(boolean enableScrolling, TYPE map)
    {
        this.map = map;
        getGameListener().addMouseMotionListener(new MouseMotionAdapter()
        {
            @Override
            public void mouseMoved(MouseEvent e)
            {
                fireMouseMotion(MouseEventType.MOVED, e);
            }
            
            @Override
            public void mouseDragged(MouseEvent e)
            {
                fireMouseMotion(MouseEventType.DRAGGED, e);
            }
        });
    }
    
    protected void fireMouseMotion(MouseEventType type, MouseEvent e)
    {
        if (isRunning())
        {
            Point newMousePoint = getMap().convertMousePointToCoord(calcRelativeMousePoint(e.getPoint()));
            if (mousePoint == null || mousePoint.equals(newMousePoint) == false)
            {
                Logger.trace("MOUSE-POINT: ", newMousePoint);
                mousePoint = newMousePoint;
            }
            
        }
    }
    
    protected TYPE getMap()
    {
        return map;
    }
    
    protected Point getMousePoint()
    {
        return mousePoint;
    }
    
    @Override
    protected final Dimension getImageSize()
    {
        return getMap().getCellMapSize();
    }
}
